<?php
defined('IN_YuLin') || exit('NO PERMIT!');
if(IS_POST){
	$userStr = NoBadStr($_POST['username']);
	$userList = explode(',', $userStr);
	$userArr = array();
	foreach ($userList as $k => $v) {
		if(!empty(trim(NoBadStr($v)))){
			array_push($userArr,"'".trim(NoBadStr($v))."'");
		}
	}
	if(!count($userArr))ShowMsg('输入有误',U('user/manualdeal'));
	$username = implode(',', $userArr);
	$money = intval($_POST['money']);
	$radio = intval($_POST['radio']);
	$remark = NoBadStr($_POST['remark']);
	$sql = "select id, money from ".Table('user')." where username in (".$username.")";
	$res = $db->getall($sql);
	if(!$res || count($res) != count($userArr))ShowMsg('会员不存在',U('user/manualdeal'));
	if(!is_numeric($money))ShowMsg('输入的金额不正确~',U('user/manualdeal'));
	($radio > 0 && $radio < 4) && $money = $money;
	if($radio > 3 && $radio < 6){
		foreach ($res as $k => $v) {
			if(intval($v['money']) < $money)ShowMsg('输入的金额超过了拥有资金',U('user/manualdeal'));
		}
		$money = -$money;
	}
	foreach ($res as $k => $v) {
		$data['uid'] = $v['id'];
		$data['balance'] = $money;
		$data['tid'] = 'manualdeal';
		$data['lkey'] = 'manualdeal_add_'.$radio.'_'.$v['id'].date('_YmdHis_').rand(1000,9999);
		$data['money'] = abs($money);
		$data['notes'] = $remark;
		$result = MoneyLog($data);
		if(!$result)ShowMsg('操作失败！',U('user/manualdeal'));
		if($result && ($k == count($res) - 1))ShowMsg('操作成功！',U('user/manualdeal'));
	}
}
$tpl->display($m.'/'.$c);